package ui
{
	import flash.display.NativeWindow;
	import flash.display.Sprite;
	import flash.display.Stage;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	import flash.geom.Rectangle;
	import flash.text.TextField;
	import flash.text.TextFormat;
	
	import ui.data.DisplayPanel;
	import ui.data.HoverButton;
	import ui.data.PanelRow;
	import ui.data.ScrollPanel;
	import ui.util.BitmapDataLoader;
	import ui.util.BitmapEvent;
	import ui.util.JSONTextureManager;

	/**
	 * This is the controler part of the MVC layout.
	 */
	public class UIManager extends EventDispatcher
	{
		private static const BACKGROUND_PATH:String = "../assets/images/Note Turtle Default.png";
		private static const JSON_PATH:String = "../assets/json/Note Turtle Default.json";

		private static const BACKGROUND_RECTANGLE:String = "Background Panel.png";
		private static const TITLE_RECTANGLE:String = "Title Bar.png";
		private static const PANEL_ROW_RECTANGLE:String = "Panel Row.png";
		
		private static const CLOSE_RECTANGLE:String = "Close.png";
		private static const MINIMIZE_RECTANGLE:String = "Minimize.png";
		private static const RESTORE_RECTANGLE:String = "Restore.png";
		private static const CLOSE_HOVER_RECTANGLE:String = "Close Hover.png";
		private static const MINIMIZE_HOVER_RECTANGLE:String = "Minimize Hover.png";
		private static const RESTORE_HOVER_RECTANGLE:String = "Restore Hover.png";
		
		public static const CLOSE:String = "Close Clicked";
		public static const MINIMIZE:String = "Minimize Clicked";
		public static const RESTORE:String = "Restore Clicked";
		
		private var activeWindow:NativeWindow;
		private var stage:Stage;
		private var width:int;
		private var height:int;
		private var currentX:int;
		private var currentY:int;
		
		private var textureManager:JSONTextureManager;
		private var bitmapLoader:BitmapDataLoader;
		
		private var background:DisplayPanel;
		private var backgroundRectangle:Rectangle;
		
		private var title:DisplayPanel;
		private var titleRectangle:Rectangle;
		
		private var titleTextField:TextField;
		
		private var scrollPanel:ScrollPanel;
		
		private var panelRowSample:PanelRow;
		private var panelRowRectangle:Rectangle;
		
		private var closeButton:HoverButton;
		private var closeButtonRectangle:Rectangle;
		private var closeButtonHoverRectangle:Rectangle;
		
		private var minimizeButton:HoverButton;
		private var minimizeButtonRectangle:Rectangle;
		private var minimizeButtonHoverRectangle:Rectangle;
		
		private var restoreButton:HoverButton;
		private var restoreButtonRectangle:Rectangle;
		private var restoreButtonHoverRectangle:Rectangle;
		
		public function UIManager(activeWindow:NativeWindow) {
			this.activeWindow = activeWindow;
			this.stage = activeWindow.stage;
			
			textureManager = new JSONTextureManager();
			bitmapLoader = new BitmapDataLoader();
		}
		
		public function createUI(windowWidth:int, windowHeight:int):void {
			this.width = windowWidth;
			this.height = windowHeight;
			
			background = new DisplayPanel();
			title = new DisplayPanel();
			titleTextField = new TextField();
			scrollPanel = new ScrollPanel();
			panelRowSample = new PanelRow();
			
			closeButton = new HoverButton();
			minimizeButton = new HoverButton();
			restoreButton = new HoverButton();
			
			textureManager.addEventListener(JSONTextureManager.COMPLETE, onJSONComplete);
			textureManager.loadJSON(JSON_PATH, "frames");
		}
		
		private function onJSONComplete(event:Event):void {
			textureManager.removeEventListener(JSONTextureManager.COMPLETE, onJSONComplete);
			
			backgroundRectangle = textureManager.getRectangle(BACKGROUND_RECTANGLE);
			titleRectangle = textureManager.getRectangle(TITLE_RECTANGLE);
			panelRowRectangle = textureManager.getRectangle(PANEL_ROW_RECTANGLE);
			
			closeButtonRectangle = textureManager.getRectangle(CLOSE_RECTANGLE);
			closeButtonHoverRectangle = textureManager.getRectangle(CLOSE_HOVER_RECTANGLE);
			
			minimizeButtonRectangle = textureManager.getRectangle(MINIMIZE_RECTANGLE);
			minimizeButtonHoverRectangle = textureManager.getRectangle(MINIMIZE_HOVER_RECTANGLE);
			
			restoreButtonRectangle = textureManager.getRectangle(RESTORE_RECTANGLE);
			restoreButtonHoverRectangle = textureManager.getRectangle(RESTORE_HOVER_RECTANGLE);
			
			createBackground();
			addEvents();
		}
		
		private function addEvents():void {
			//addDragEvents();
			addButtonEvents();
		}
		
		private function addDragEvents():void {
			title.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		private function addButtonEvents():void {
			closeButton.addListeners();
			minimizeButton.addListeners();
			restoreButton.addListeners();
			
			closeButton.addEventListener(MouseEvent.CLICK, onCloseClick);
			minimizeButton.addEventListener(MouseEvent.CLICK, onMinimizeClick);
			restoreButton.addEventListener(MouseEvent.CLICK, onRestoreClick);
		}
		
		private function onMouseDown(event:MouseEvent):void {
			currentX = stage.mouseX;
			currentY = stage.mouseY;
			
			stage.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			//titleBitmap.removeEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		private function onMouseUp(event:MouseEvent):void {
			stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
			stage.removeEventListener(MouseEvent.MOUSE_UP, onMouseUp);
			//titleBitmap.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
		}
		
		private function onMouseMove(event:MouseEvent):void {
			activeWindow.x += stage.mouseX - currentX;
			activeWindow.y += stage.mouseY - currentY;
			
			currentX = stage.mouseX;
			currentY = stage.mouseY;
		}
		
		private function onCloseClick(event:MouseEvent):void {
			dispatchEvent(new Event(CLOSE));
		}
		
		private function onMinimizeClick(event:MouseEvent):void {
			dispatchEvent(new Event(MINIMIZE));
		}
		
		private function onRestoreClick(event:MouseEvent):void {
			dispatchEvent(new Event(RESTORE));
		}
		
		private function createBackground():void {
			bitmapLoader.addEventListener(BitmapEvent.COMPLETE, loadBackground);
			bitmapLoader.loadBitmapData(BACKGROUND_PATH);
		}
		
		private function loadBackground(event:BitmapEvent):void {
			bitmapLoader.removeEventListener(BitmapEvent.COMPLETE, loadBackground);
			var defaultPoint:Point = new Point(0, 0);
			
			background.setDefaultRectangle(backgroundRectangle);
			background.setSheet(event.bitmap);
			background.draw();
			
			title.setDefaultRectangle(titleRectangle);
			title.setSheet(event.bitmap);
			title.draw();
			
			panelRowSample.setDefaultRectangle(panelRowRectangle);
			panelRowSample.setSheet(event.bitmap);
			panelRowSample.draw();
			
			closeButton.setDefaultRectangle(closeButtonRectangle);
			closeButton.setHoverRectangle(closeButtonHoverRectangle);
			closeButton.setSheet(event.bitmap);
			closeButton.draw();
			
			minimizeButton.setDefaultRectangle(minimizeButtonRectangle);
			minimizeButton.setHoverRectangle(minimizeButtonHoverRectangle);
			minimizeButton.setSheet(event.bitmap);
			minimizeButton.draw();
			
			restoreButton.setDefaultRectangle(restoreButtonRectangle);
			restoreButton.setHoverRectangle(restoreButtonHoverRectangle);
			restoreButton.setSheet(event.bitmap);
			restoreButton.draw();
			
			var textFormat:TextFormat = new TextFormat();
			textFormat.font = "action man";
			textFormat.size = 14;
			
			titleTextField.defaultTextFormat = textFormat;
			titleTextField.text = "Note Turtle";
			
			scrollPanel.setNumberOfRows(3);
			scrollPanel.setPanelRow(panelRowSample);
			
			scrollPanel.addRow(cloneRow());
			scrollPanel.addRow(cloneRow());
			scrollPanel.addRow(cloneRow());
			scrollPanel.addRow(cloneRow());
			
			stage.addChild(background);
			stage.addChild(title);
			stage.addChild(closeButton);
			stage.addChild(minimizeButton);
			stage.addChild(restoreButton);
			stage.addChild(scrollPanel);
			stage.addChild(titleTextField);
			
			positionateBackground();
		}
		
		private function positionateBackground():void {
			closeButton.x = stage.width - closeButton.width - 8;
			closeButton.y = 2;
			
			minimizeButton.x = closeButton.x - closeButton.width - 2;
			minimizeButton.y = 2;
			
			restoreButton.x = minimizeButton.x - minimizeButton.width - 2;
			restoreButton.y = 2;
			
			scrollPanel.x = 10;
			scrollPanel.y = title.height + 10;
		}
		
		private function cloneRow():PanelRow {
			var instance:PanelRow = new PanelRow();
			instance.setDefaultRectangle(panelRowSample.getDefaultRectangle());
			instance.setHoverRectangle(panelRowSample.getHoverRectangle());
			instance.setSheet(panelRowSample.getSheet());
			
			return instance;
		}
	}
}